package com.ifonly.common.typeconverter.impl;

import com.ifonly.common.typeconverter.Converter;
import com.ifonly.common.typeconverter.TypeConversionException;

import java.math.BigInteger;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-17 21:55
 * @since JDK 1.6
 */
public class BigIntegerConverter implements Converter<BigInteger> {

    public BigInteger convert(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof BigInteger) {
            return (BigInteger) value;
        }
        if (value instanceof Number) {
            return new BigInteger(String.valueOf(((Number)value).longValue()));
        }
        try {
            return new BigInteger(value.toString().trim());
        } catch (NumberFormatException nfex) {
            throw new TypeConversionException(value, nfex);
        }
    }

}